<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="1000px"
      :close-on-click-modal="false"
      :lock-scroll="false"
      @closed="resetForm('form')"
    >
      <div v-loading="loading" class="dialogSpan">
        <div>
          <el-form ref="form" :rules="rules" :model="form" label-width="120px">
            <el-row>
              <!-- <el-col :span="24">
                <el-form-item label="标题" prop="title">
                  <el-input v-model.trim="form.title"></el-input>
                </el-form-item>
              </el-col> -->
              <!-- <el-col :span="12">
                <el-form-item label="我的姓名" prop="userName">
                  <el-input v-model.trim="form.userName"></el-input>
                </el-form-item>
              </el-col> -->
              <!-- <el-col :span="12">
                <el-form-item label="联系电话" prop="phone">
                  <el-input v-model.trim="form.phone"></el-input>
                </el-form-item>
              </el-col> -->
              <!-- <el-col :span="12">
                <el-form-item label="电子邮箱" prop="email">
                  <el-input v-model.trim="form.email"></el-input>
                </el-form-item>
              </el-col> -->
              <!-- <el-col :span="12">
                <el-form-item label="联系地址">
                  <el-input v-model.trim="form.addr"></el-input>
                </el-form-item>
              </el-col> -->
            </el-row>
            <el-form-item label="纠错网页地址" prop="url">
              <el-input v-model.trim="form.url"></el-input>
            </el-form-item>
            <el-form-item label="错误类型" prop="errorType">
              <el-radio v-model="form.errorType" label="错别字"
                >错别字</el-radio
              >
              <el-radio v-model="form.errorType" label="图片显示错误"
                >图片显示错误</el-radio
              >
              <el-radio v-model="form.errorType" label="页面格式错误"
                >页面格式错误</el-radio
              >
              <el-radio v-model="form.errorType" label="其他错误"
                >其他错误</el-radio
              >
            </el-form-item>
            <el-form-item label="纠错内容" prop="content">
              <UEditor
                ref="ueditor"
                :ueHeight="320"
                :defaultMsg="form.content"
              />
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button type="custom" v-preventReClick @click="submitForm('form')"
            >提 交</el-button
          >
          <el-button @click="dialogVisible = false">取 消</el-button>
        </span>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  tbSysCorrectionAdd,
  tbSysCorrectionUpdate,
} from '@/api/admin/system/correction.js'
import Bus from '@/utils/bus.js'
export default {
  data() {
    return {
      loading: true,
      title: '填写纠错',
      dialogVisible: false,
      addOrEdit: 1,
      checkedData: {},
      form: {
        status: '0',
        content: '',
        source: '',
        url: '',
        errorType: '错别字',
        phone: '',
        title: '',
        email: '',
        addr: '',
        userName: '',
      },
      rules: {
        url: [
          { required: true, message: '请输入错误资源地址', trigger: 'blur' },
        ],
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        content: [
          { required: true, message: '请输入纠错内容', trigger: 'blur' },
        ],
        errorType: [
          { required: true, message: '请输入纠错依据', trigger: 'blur' },
        ],
        userName: [
          { required: true, message: '请输入我的姓名', trigger: 'blur' },
        ],
      },
    }
  },
  mounted() {
    Bus.$on('showDialogCorrectionAdd', (v) => {
      if (v) {
        this.checkedData = v
        this.handleAddOrEdit(2)
      } else {
        this.handleAddOrEdit(1)
      }
    })
  },
  beforeDestroy() {
    Bus.$off('showDialogCorrectionAdd')
  },
  methods: {
    saveFile(v) {
      this.form.fileId = v[0].id
    },
    //显示弹窗
    handleAddOrEdit(type) {
      if (type == 2) {
        this.title = '回复意见'
        for (let i in this.checkedData) {
          if (this.form.hasOwnProperty(i)) {
            this.form[i] = this.checkedData[i] || ''
          }
        }
        this.form.id = this.checkedData.id
        this.form.status = 1
      } else {
        this.title = '填写纠错'
        delete this.form.id
        this.form.url = window.location.href
      }
      this.addOrEdit = type

      this.dialogVisible = true
      this.loading = false
    },
    submitForm(form) {
      this.form.content = this.$refs.ueditor.message
      //this.form.source = this.$refs.ueditor2.message;
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.addOrEdit == 1 ? this.add() : this.edit()
        }
      })
    },
    //新增
    add() {
      tbSysCorrectionAdd(this.form).then((res) => {
        this.dialogVisible = false
        Bus.$emit('refreshCorrectionList')
        this.$message({
          message: '提交成功',
          type: 'success',
        })
      })
    },
    //编辑
    edit() {
      tbSysCorrectionUpdate(this.form).then((res) => {
        this.dialogVisible = false
        Bus.$emit('refreshCorrectionList')
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
    //重置表单
    resetForm(form) {
      this.$refs.ueditor.message = ''
      this.$refs[form].resetFields()
      for (let key in this.form) {
        this.form[key] = ''
        this.form.status = '0'
        this.form.errorType = '错别字'
      }
    },
  },
}
</script>
<style scoped>
.dialogSpan span.dialog-footer {
  text-align: right;
  display: inherit;
}
</style>